import { Component } from "@angular/core";
import { NavController,NavParams } from 'ionic-angular';
import { NativeStorage } from '@ionic-native/native-storage';
import { HttpClient } from '@angular/common/http'

import { AppConfig } from '../../app/app.config';
import { PayresultPage } from "./payresult/payresult";

import { PayPal, PayPalPayment, PayPalConfiguration } from '@ionic-native/paypal';
@Component({
    selector: 'page-pay',
    templateUrl: 'pay.html',
})

export class PayPage{
    payresultPage = PayresultPage;
    type = 1;
    info:any;
    form={
        deposit: false,
        deposit_unit: "USD",
        other_price: false,
        other_unit: "USD",
        price: 0,
        price_unit: "USD",
        total_price: '0'
    };
    constructor(
        private navCtl: NavController,
        private payPal: PayPal,
        private navParams: NavParams,
        private stroge:NativeStorage,
        private http:HttpClient
    ) {
        this.info = this.navParams.get('data');
        console.log(this.info)
    }
    _unit(item){
        switch(item){
            case 'GBP':
                return '£';
            case 'EUR':
                return '€';
            case 'USD':
                return '$';
            case 'RMB':
                return '¥';
        }
    }
    ionViewDidEnter(){
        this.stroge.getItem('token').then(token=>{
            this.http.get(AppConfig.getUrl()+'Cart/getPay?token='+token+'&id='+this.info.id).subscribe(res=>{
                if(res['code']==101){
                    this.form = res['data']
                }
            })
        })
    }
    pay(){
        this.payPal.init({
            PayPalEnvironmentProduction: '',
            PayPalEnvironmentSandbox: 'AV2-K-YHJf3FgLJ3UiDNqA6z0OkZ6Y_v6OxzSBMJ5W12tBoW8K6aGVf0M6PCkDcTxz2f5jZNV_JtcZF5'
          }).then(() => {
            // Environments: PayPalEnvironmentNoNetwork, PayPalEnvironmentSandbox, PayPalEnvironmentProduction
            this.payPal.prepareToRender('PayPalEnvironmentSandbox', new PayPalConfiguration({
              // Only needed if you get an "Internal Service Error" after PayPal login!
              //payPalShippingAddressOption: 2 // PayPalShippingAddressOptionPayPal
            })).then(() => {
                let payment = new PayPalPayment(this.form.total_price, this.form.price_unit, this.info.info.address, 'sale');
                payment.invoiceNumber = this.info.cart+',cart';
                this.payPal.renderSinglePaymentUI(payment).then((res) => {
                    this.navCtl.pop();
                }, () => {
                });
            }, () => {
            });
          }, () => {
            
        });
    }
}